package com.itgaohe.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itgaohe.dao.entity.User;
import com.itgaohe.dao.entity.UserInfo;
import com.itgaohe.dto.resp.SearchUserFriendsDTO;
import com.itgaohe.dto.resp.SearchUserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {


    // 根据当前用户的id查询他的粉丝列表（别人关注了他）
    @Select({
            "SELECT",
            "u.id,",
            "u.nick_name AS name,",
            "u.icon,",
            "u.fans,",
            "u.followee,",
            "CASE WHEN f.id IS NOT NULL THEN 0 ELSE 1 END AS isFollow",
            "FROM tb_follow f",
            "JOIN tb_user u ON f.user_id = u.id",
            "WHERE cursor_index < #{cursor} AND f.follow_user_id = #{userId} AND u.is_deleted = 0 "+
                    "ORDER BY cursor_index DESC LIMIT 10"
    })
    List<SearchUserInfo> selectAllFansByUserId(
            @Param("userId") Long userId,
            @Param("cursor") Long cursor
    );

    // 根据当前用户的id查询他关注的用户列表（他关注了谁）
    @Select({
            "SELECT",
            "u.id,",
            "u.nick_name AS name,",
            "u.icon,",
            "u.fans,",
            "u.followee,",
            "CASE WHEN f.id IS NOT NULL THEN 0 ELSE 1 END AS isFollow",
            "FROM tb_follow f",
            "JOIN tb_user u ON f.follow_user_id = u.id",
            "LEFT JOIN tb_user_info i ON u.id = i.user_id",
            "WHERE cursor_index < #{cursor} AND f.user_id = #{userId} AND u.is_deleted = 0 "+
                    "ORDER BY cursor_index DESC LIMIT 10"
    })
    List<SearchUserInfo> selectAllFollowersByUserId(
            @Param("userId") Long userId,
            @Param("cursor") Long cursor
    );


    //查询用户详细信息
    @Select("SELECT * FROM tb_user_info " +
            "WHERE user_id = #{userId}")
    List<UserInfo> selectAllUserInfoByUserId(@Param("userId") Long userId);


    /*
    根据用户的名字来进行模糊查询
    * */
    @Select("SELECT " +
            "id, " +
            "nick_name as name , " +
            "icon, " +
            "fans , " +
            "followee , " +
            "cursor_index " +
            "FROM tb_user " +
            "WHERE cursor_index < #{cursor} " +
            "AND is_deleted = 0 " +
            "AND nick_name LIKE CONCAT('%', #{nickName}, '%')" +
            "ORDER BY cursor_index DESC LIMIT 10 ")
    List<SearchUserInfo> searchUsersByNickName(
            @Param("nickName") String nickName,
            @Param("cursor") Long cursor);
    /*
    根据用户Id查询好友信息
    * */
    @Select({
            "SELECT",
            "u.id AS id,",
            "u.nick_name AS name,",
            "u.icon,",
            "u.fans,",
            "u.followee",
            "FROM tb_follow f1",
            "INNER JOIN tb_follow f2",
            "ON f1.user_id = f2.follow_user_id AND f1.follow_user_id = f2.user_id",
            "INNER JOIN tb_user u ON f1.follow_user_id = u.id",
            "WHERE f1.user_id = #{userId} ",
            "AND u.id != #{userId} ",
            "AND u.cursor_index < #{cursor}",
            "ORDER BY u.cursor_index DESC LIMIT 10"
    })
    List<SearchUserInfo> selectAllFriendsByUserId(
            @Param("userId") Long userId,
            @Param("cursor") Long cursor
    );



}
